const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    @property([cc.Node])
    bg_subnode:cc.Node[] = [];

    private dt_count_temp:number = 1/40;   //40帧   
    private dt_count:number = 0;           //计时器
    private cur_bg = null;                 //摄像机下的背景
    private speed = 200;                   //背景的移动速度
    private game_flag:boolean = true;      //游戏开始标志
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.node.on('bg_roll',this.bg_roll.bind(this),true);
        console.log('this.bg_subnode[0]:',this.bg_subnode[0]);
        console.log('this.bg_subnode[1]:',this.bg_subnode[1]);
        this.cur_bg = this.bg_subnode[0];
    }

    bg_roll(status){
        console.log('bg_roll:',status,this.game_flag);
        this.game_flag = !this.game_flag;
    }

    start () {

    }

    update_function(dt){
        if(this.game_flag == true)
        {
            var s = dt * this.speed;
            this.bg_subnode[0].x -= s;
            this.bg_subnode[1].x -= s;
            if (this.cur_bg.x <= -750) { // 地图切换
                if(this.cur_bg == this.bg_subnode[1]) {
                    this.bg_subnode[1].x = this.bg_subnode[0].x + 750;
                    this.cur_bg = this.bg_subnode[0];
                }
                else {
                    this.bg_subnode[0].x = this.bg_subnode[1].x + 750;
                    this.cur_bg = this.bg_subnode[1];
                }
            }
        }       
    }

    update (dt) {
        this.dt_count = this.dt_count + dt;
        if(this.dt_count >= this.dt_count_temp)
        {
            this.update_function(dt);
            this.dt_count = 0;
        }
    }
}
